#include "ti_msp_dl_config.h"
#include "stdlib.h"
#include "motor.h"

/********************全局函数定义********************/

/**
 * @brief	设置右电机pwm输出
 * @param	ccr值
 */
void r_motor_pwm_set(int32_t ccr)
{
	uint16_t pwm_ccr = (uint16_t)abs(ccr);
	
	if(ccr > 0)
	{
		DL_TimerA_setCaptureCompareValue(MOTOR_PWM_INST, pwm_ccr, GPIO_MOTOR_PWM_C2_IDX);
		DL_TimerA_setCaptureCompareValue(MOTOR_PWM_INST, 0, GPIO_MOTOR_PWM_C3_IDX);
	}
	else if(ccr < 0)
	{
		DL_TimerA_setCaptureCompareValue(MOTOR_PWM_INST, pwm_ccr, GPIO_MOTOR_PWM_C3_IDX);
		DL_TimerA_setCaptureCompareValue(MOTOR_PWM_INST, 0, GPIO_MOTOR_PWM_C2_IDX);
	}
	else
	{
		DL_TimerA_setCaptureCompareValue(MOTOR_PWM_INST, 0, GPIO_MOTOR_PWM_C2_IDX);
		DL_TimerA_setCaptureCompareValue(MOTOR_PWM_INST, 0, GPIO_MOTOR_PWM_C3_IDX);
	}

	return ;
}

/**
 * @brief	设置左电机pwm输出
 * @param	ccr值
 */
void l_motor_pwm_set(int32_t ccr)
{
	uint16_t pwm_ccr = (uint16_t)abs(ccr);
	
	if(ccr > 0)
	{
		DL_TimerA_setCaptureCompareValue(MOTOR_PWM_INST, pwm_ccr, GPIO_MOTOR_PWM_C1_IDX);
		DL_TimerA_setCaptureCompareValue(MOTOR_PWM_INST, 0, GPIO_MOTOR_PWM_C0_IDX);
	}
	else if(ccr<0)
	{
		DL_TimerA_setCaptureCompareValue(MOTOR_PWM_INST, pwm_ccr, GPIO_MOTOR_PWM_C0_IDX);
		DL_TimerA_setCaptureCompareValue(MOTOR_PWM_INST, 0, GPIO_MOTOR_PWM_C1_IDX);
	}
	else
	{
		DL_TimerA_setCaptureCompareValue(MOTOR_PWM_INST, 0, GPIO_MOTOR_PWM_C0_IDX);
		DL_TimerA_setCaptureCompareValue(MOTOR_PWM_INST, 0, GPIO_MOTOR_PWM_C1_IDX);
	}
}
